package foo.bar.wiki.radeox;

import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.domain.Resource;
import foo.bar.wiki.web.action.general.ResourceRetrievalActionBean;
import foo.bar.radeox.api.engine.context.InitialRenderContext;
import foo.bar.radeox.api.engine.context.RenderContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sourceforge.stripes.util.UrlBuilder;

import java.util.Locale;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class WikiSyntaxHelpExampleRenderEngine extends DefaultWikiRenderEngine {

    private static final Log LOG = LogFactory.getLog(WikiSyntaxHelpExampleRenderEngine.class);



    public WikiSyntaxHelpExampleRenderEngine(WikiService wikiService, InitialRenderContext initialRenderContext) {
        super(null, initialRenderContext);
    }

            public boolean exists(String pageName, RenderContext renderContext) {
                return "MyLittlePage".equals(pageName);
            }

            public String include(String name) {
                if ("MyLittlePage".equals(name)) {
                    return render("  **THIS IS THE CONTENT OF MY LITTLE PAGE** ~~Enjoy !!~~");
                }
                return "<span class='error'>Cannot find page with name ["+name+"]</span>";  // nothing to include
            }

        public String includeResource(String name) {
            UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), "/wikiThemes/default/images/sampleForWikiSyntaxHelp.jpg", true);
            LOG.warn(urlBuilder.toString());
            return "<img src='"+urlBuilder.toString()+"'/>";
        }



            public void appendLink(StringBuffer buffer, String name, String view, String anchor, RenderContext renderContext) {
                if ("MyLittlePage".equals(name)) { // page exists
                    buffer.append("<a href='#' onclick='linkToPageDoesNotWorkItsAnExample();'>"+name+"</a>");
                }
                else {
                    appendCreateLink(buffer, name, view, renderContext);
                }
            }

            public void appendLink(StringBuffer buffer, String name, String view, RenderContext renderContext) {
                appendLink(buffer, name, view, null, renderContext);
            }

            public void appendCreateLink(StringBuffer buffer, String name, String view, RenderContext renderContext) {
                buffer.append(name+"<a href='#' onclick='createLinkDoesNotWorkItsAnExample();'>?</a>");
            }
}
